package com.ruoyi.quartz.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.domain.AttendanceRecord;
import com.ruoyi.quartz.domain.AttendanceStatistics;
import com.ruoyi.common.domain.Emply;

import java.util.*;
import java.util.concurrent.CyclicBarrier;
import java.util.stream.Collectors;

public class GroupTask extends Thread{

	private CyclicBarrier barrier;
	private List<AttendanceRecord> attendanceRecordList;
	private List<AttendanceStatistics> attendanceStatisticsList;
	private String text;
	private List<Emply> emplyList;

	public GroupTask(CyclicBarrier barrier, List<AttendanceRecord> attendanceRecordList, List<AttendanceStatistics> attendanceStatisticsList, String text, List<Emply> emplyList){
		this.barrier = barrier;
		this.attendanceRecordList = attendanceRecordList;
		this.attendanceStatisticsList = attendanceStatisticsList;
		this.text = text;
		this.emplyList = emplyList;
	}


	public void run(){
		Map<String, List<AttendanceRecord>> attendanceRecordMap = new HashMap<>();
		try {
			if(attendanceRecordList != null && attendanceRecordList.size() > 0){
				attendanceRecordMap = attendanceRecordList.stream().sorted(Comparator.comparing(AttendanceRecord::getCardTime)).collect(Collectors.groupingBy(AttendanceRecord::getSysNo));
			}
			for (Emply p: emplyList) {
				List<AttendanceRecord> attendanceRecords = attendanceRecordMap.get(p.getSysNo());
				AttendanceStatistics statistics = new AttendanceStatistics();
				statistics.setSysNo(p.getSysNo());
				statistics.setDeptId(p.getDepartId());//部门ID
				statistics.setDeptName(p.getDepartName());//部门名称
				statistics.setEmployeeId(p.getEmployeeId());//员工ID
				statistics.setEmpName(p.getEmployeeName());//员工姓名
				statistics.setCalDate(DateUtils.parseDate(text));//打卡日期
				statistics.setCalDateText(text);
				if(attendanceRecords != null && attendanceRecords.size() > 0){
					AttendanceRecord first = attendanceRecords.get(0);
					AttendanceRecord end = null;
					if(attendanceRecords.size() > 1){
						end = attendanceRecords.get(attendanceRecords.size() - 1);
						statistics.setDrawtmbgn(first.getCardTime());//上班打卡时间
						statistics.setDrawtmend(end.getCardTime());//下班打卡时间
						statistics.setDrawtmbgnText(DateUtils.parseDateToStr(DateUtils.HH_MM_SS, first.getCardTime()));
						statistics.setDrawtmendText(DateUtils.parseDateToStr(DateUtils.HH_MM_SS, end.getCardTime()));
					} else {
						statistics.setStatus(2);//异常
						end = first;
						//判断未签入/未签出
						if(judegStatus(first.getCardTime())){
							statistics.setDrawtmbgnText(DateUtils.parseDateToStr(DateUtils.HH_MM_SS, first.getCardTime()));
							statistics.setDrawtmendText("未签出");
						} else {
							statistics.setDrawtmendText(DateUtils.parseDateToStr(DateUtils.HH_MM_SS, first.getCardTime()));
							statistics.setDrawtmbgnText("未签入");
						}
					}
					long min = DateUtils.getMin(end.getCardTime(), first.getCardTime());
					statistics.setFactGs((int)min);

					if(judegDate(first.getCardTime(), end.getCardTime()) && min >= 510){
						statistics.setStatus(1);
					} else {
						statistics.setStatus(2);
					}
				} else {
					statistics.setDrawtmendText("未签出");
					statistics.setDrawtmbgnText("未签入");
					statistics.setStatus(2);
					statistics.setFactGs(0);
				}
				attendanceStatisticsList.add(statistics);
			};
			System.out.println("-----------------------------------" +  text  + "----------------------------------------------");
			barrier.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean judegDate(Date startDate, Date endDate){
		String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startDate);
		Date startWorkDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, date + " 09:00:59");
		Date endWorkDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, date + " 17:00:00");
		if(startDate.getTime() <= startWorkDate.getTime() && endDate.getTime() >= endWorkDate.getTime()){
			return true;
		} else {
			return false;
		}
	}

	public boolean judegStatus(Date startDate){
		String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startDate);
		Date endWorkDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, date + " 17:00:00");

		if(startDate.getTime() < endWorkDate.getTime()){
			return true;
		} else {
			return false;
		}
	}

}